package com.adopapa.ethexplorer.service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.adopapa.ethexplorer.conf.AppContextUtil;
import com.adopapa.ethexplorer.conf.RedisTemplateManager;
import com.adopapa.ethexplorer.utils.Tools;

@Service
public class RedisService extends BaseService {

	/**
	 * 获取redisTemplate
	 */
	@SuppressWarnings("unchecked")
	public RedisTemplate<String, Object> getRedisTemplate(int dbIndex) {
		RedisTemplateManager<String, Object> redisTemplateManager = //
				AppContextUtil.getAppContext().getBean(RedisTemplateManager.class);

		return redisTemplateManager.getRedisTemplate(dbIndex);

	}

	/**
	 * 从缓存中获得用户信息
	 */
	public Map<String, String> getUserInfo(String loginToken) {
		RedisTemplate<String, Object> redisTemplate = getRedisTemplate(0);
		@SuppressWarnings("unchecked")
		Map<String, String> userInfo = //
				(Map<String, String>) redisTemplate.opsForValue().get(loginToken);
		if (!Tools.empty(userInfo)) {
			redisTemplate.opsForValue().set(loginToken, userInfo, 24 * 3600L, TimeUnit.SECONDS);
		}
		return userInfo;
	}

}
